// pages/index/decorcompany/livesite_detail.js
var call = require("../../../utils/request.js")
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidecode: true,
    showcode: false,
    cards: true,
    sid: '',
    card: '获取验证码',
    landing_page: '',
    basepeak: '',
    infotele:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    console.log(options)
    var picid = options.picid
    wx.request({
      url: app.apiUrl("c=buildingsite/index&a=getdetail"),
      data: {
        album_id: picid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'GET',
      success: function(res) {
        var polists = res.data.datas.detail_info
        var process = res.data.datas.schedule_data
        var loading = polists.schedule_conifg
        var loadsta = parseInt(polists.schedule) 
        console.log(res.data)
        //页面两张图片展示 
        var imgs =[]
        var imgt =[]
        var state = []
        for (var i in process) {
          var length = process[i].schedule_list.length
          if (length < 2) {
            imgs.push(process[i].schedule_list[0].picture_url)
            state.push(process[i].schedule_list[0])
            process[i].imgs = imgs[i];
            process[i].imgt = imgs[i];
            process[i].state = false
          } 
          if (length >=2){
            imgs.push(process[i].schedule_list[0].picture_url)
            imgt.push(process[i].schedule_list[1].picture_url)
            process[i].imgs = imgs[i];
            process[i].imgt = process[i].schedule_list[1].picture_url;
            process[i].state = true
          }
        }
        //头部工程进度实现
        var loadings = []
        for (let prop  in loading) {
          loadings.push({
            num: prop,
            name: loading[prop]
          })
        }
        console.log(loadings);
        var length =loadings.length-1
        for (var i in loadings){
          loadings[0].start = 'white'
          loadings[0].end = 'black'
          loadings[0].icon = 'https://img.xtuan.com/xcximgtest/images/decormpany/live_detail/complete.png'
          //loadings[length].satr = 'white'
          for (var j = 1; j < loadsta;j++){
            loadings[j].start = 'black'
            loadings[j].end = 'black'
            loadings[j].icon = 'https://img.xtuan.com/xcximgtest/images/decormpany/live_detail/complete.png'
            loadings[length].end = 'white'
          }
          for (var k =loadsta; k <length;k++){
            loadings[k].start = '#EFF3F6'
            loadings[k].end = '#EFF3F6'
            loadings[k].icon = 'https://img.xtuan.com/xcximgtest/images/decormpany/live_detail/nocomplete.png'
          }
          if (loadsta-1 ==length){
            loadings[length].start = 'black'
            loadings[length].end = '#fff'
            loadings[length].icon = 'https://img.xtuan.com/xcximgtest/images/decormpany/live_detail/finished.png'
          }else{
            loadings[length].start = '#EFF3F6'
            loadings[length].end = '#fff'
            loadings[length].icon = 'https://img.xtuan.com/xcximgtest/images/decormpany/live_detail/nocomplete.png'
          }
        }
        // console.log(process)
        // console.log(loadings)
        that.setData({
          polist: polists,
          process: process,
          loading:loadings
        })
        wx.setNavigationBarTitle({
          title: polists.title //页面标题为路由参数
        })
      }
    })
    call.getData('session', this.shuffleSuc, this.fail);
    setTimeout(function() {
      that.initcode()
    }, 200)
  },
  //获取session
  shuffleSuc: function(res) {
    var that = this;
    var sid = res.data.PHPSESSID
    wx.setStorage({ //存储到本地
      key: "sid",
      data: sid
    })
    that.setData({
      sid: sid
    })
  },
  fail: function() {
    console.log("失败")
  },
  //初始获取验证码
  initcode: function() {
    var that = this
    var sid = that.data.sid
    var text = '获取验证码'
    wx.request({
      url: app.apiUrl("c=zhaobiao/index&a=view_visit"),
      data: {},
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': 'PHPSESSID=' + sid
      },
      method: 'GET',
      success: function(res) {
        var code = res.data.datas.code
        var baskpage = res.data.datas.landing_page
        var count =res.data.datas.bid_count
        that.setData({
          card: code,
          hidecode: false,
          showcode: true,
          basepeak: baskpage,
          count: count
        })
        setTimeout(function() {
          that.setData({
            card: text,
          })
        }, 30000)
      }
    })
  },

  //点击放大查看图片
  lookpicture:function(e){
    var that = this
    var process = that.data.process
    var ids = parseInt(e.currentTarget.id) 
    var picture = process[ids].schedule_list
    var now = e.currentTarget.dataset.one
    console.log(ids)
    console.log(now)
    if ((picture.length>1)&&(now==2)){
        that.setData({
          currentSwiper:1
        })
    } else if ((picture.length >1) && (now == 1)){
      that.setData({
        currentSwiper: 0
      })
    } else if (picture.length = 1){
      that.setData({
        currentSwiper: 0
      })
    }
    that.setData({
      pictures:picture,
      mask: true,
      showimg: true,
    })
  },
  //预约购买操作
  appointment: function() {
    this.setData({
      showinfo: true,
      mask: true,
      hidemap: true,
      showimg: false
    })
  },
  hidemask: function() {
    this.setData({
      showinfo: false,
      mask: false,
      hidemap: false,
      showimg: false,
    })
  },
  //输入号码时，预约输入判断
  bindKeyInput: function(e) {
    var value = e.detail.value
    var info = 1
    this.setData({
      info: info,
      hidecode: false,
      showcode: true,
      infotele: value
    })
  },
  settime: function() {
    var that = this
    setTimeout(function() {
      that.setData({
        alertmess: false
      })
    }, 2000)
  },
  //获取验证码
  getCode: function(e) {
    var that = this
    var text = '获取验证码'
    var tele = that.data.infotele
    var card = that.data.card
    var sid = that.data.sid
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (tele == '') {
      that.setData({
        alertmess: true,
        message: '请输入手机号码！'
      })
      that.settime()
    } else if (tele.length != 11) {
      that.setData({
        alertmess: true,
        message: '请重新输入手机号码！'
      })
      that.settime()
    } else if (!myreg.test(tele)) {
      that.setData({
        alertmess: true,
        message: '手机号错误，请重新输入！'
      })
      that.settime()
    } else {
      wx.request({
        url: app.apiUrl('c=zhaobiao/index&a=get_captcha'),
        data: {},
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Cookie': 'PHPSESSID=' + sid
        },
        method: "GET",
        success: function(res) {
          //console.log(res.data)
          var cards = res.data.datas.code
          //var landing_page = res.data.datas.landing_page
          console.log(cards)
          that.setData({
            card: cards,
            distate: false
          })
          setTimeout(function() {
            that.setData({
              card: text,
              distate: false
            })
          }, 60000)
        },
        fail: function(res) {
          console.log('请求失败', res.data)
        }
      })
    }
  },
  formSubmit: function(e) {
    var that = this
    var mobile = e.detail.value.mobile
    var code = e.detail.value.code
    var id = that.data.tid
    var thecode = that.data.card
    var sid = that.data.sid
    var basepeak = that.data.basepeak
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    // console.log('提交', sid)
    // console.log(thecode)
    var id = 1
    var title = '预约成功'
    var tsmall = '"恭喜您，预约成功，我们将尽快与您联系!'
    if (mobile == '') {
      that.setData({
        alertmess: true,
        message: '手机号码不能为空！'
      })
      that.settime()
    } else if (mobile.length != 11) {
      that.setData({
        alertmess: true,
        message: '请重新输入手机号码！'
      })
      that.settime()
    } else if (!myreg.test(mobile)) {
      that.setData({
        alertmess: true,
        message: '手机号错误，请重新输入！'
      })
      that.settime()
    } else if (code == '') {
      that.setData({
        alertmess: true,
        message: '验证码不能为空！'
      })
      that.settime()
    } else if (code != thecode) {
      that.setData({
        alertmess: true,
        message: '验证码不对，请重新输入！'
      })
      that.settime()
    } else {
      wx.request({
        url: app.apiUrl('c=zhaobiao/index&a=submit_visit'),
        data: {
          phone: mobile,
          code: code,
          company_id: id,
          landing_page: basepeak
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Cookie': 'PHPSESSID=' + sid
        },
        method: "POST",
        success: function(res) {
          //console.log(res.data)
          var code = res.data.code
          var message = res.data.error
          if (code == 200) {
            wx.navigateTo({
              url: '../free/applysuccess?data=' + [id, title, tsmall],
            })
          } else {
            that.setData({
              alertmess: true,
              message: message
            })
            that.settime()
          }
        },
        fail: function(res) {
          console.log('请求失败', res.data)
        }
      })
      this.setData({
        showModal: false,
        Modalcontent: false,
        showinfo: false,
        mask: false
      })
    }
    //console.log('form发生了submit事件，携带数据为：', e.detail.value)
  },
 
})